package it.java.giw.dataextraction.aleph.helper;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class Evaluator{


	public static void main(String[] args) {

		BufferedReader id2url = null;
		List<String[]> extractorCSV = new ArrayList<String[]>();
		List<String[]> alephredCsv = new ArrayList<String[]>();
		CSVHelper helper = new CSVHelper();

		int relevanceThreshold;

		if(args.length < 3){
			System.err.println("ERRORE: Il numero dei parametri è insufficiente! Consultare il file README.txt");
			return;
		}


		double documentiRilevantiRecuperati = 0;
		double documentiNonRilevanti = 0;

		try { 

			extractorCSV = helper.readCsv(args[0], ',');
			alephredCsv = helper.readCsv(args[1], '\t');
			relevanceThreshold = Integer.parseInt(args[2]);


//			extractorCSV = helper.readCsv("COMINGSOON_ImportIO.csv", ',');
//			alephredCsv = helper.readCsv("alephred_coomingsoon.csv", '\t');
//			relevanceThreshold = 4;


			for(String[] extractor_row : extractorCSV){
				String extractorUrl = extractor_row[9];
				id2url = new BufferedReader(new FileReader("id2url.txt"));
				String id2url_CurrentLine;
				while((id2url_CurrentLine = id2url.readLine()) != null){
					String[] id2url_array = id2url_CurrentLine.split("-->");
					String id2url_id = id2url_array[0];
					String id2url_url = id2url_array[1];
					if(id2url_url.equalsIgnoreCase(extractorUrl)){
						String[] alephredRow = findRowByID(alephredCsv, id2url_id);
						if(isRelevant(alephredRow, extractor_row, relevanceThreshold)){
							documentiRilevantiRecuperati += 1;
						}else{
							documentiNonRilevanti += 1;
						}
						break;
					}
				}
			}

			//			double documentiRecuperati = documentiRilevanti+documentiNonRilevanti;

			double documentiRecuperati = extractorCSV.size();
			double totaleRilevanti = alephredCsv.size();

			double recall = documentiRilevantiRecuperati/totaleRilevanti;
			double precision = documentiRilevantiRecuperati/documentiRecuperati;
			double fmeasure = (2*(precision)*(recall))/(recall+precision);

			System.out.println("Documenti recuperati: " + documentiRecuperati);
			System.out.println("Documenti rilevanti totali: " + totaleRilevanti);
			System.out.println("Documenti rilevanti recuperati: " + documentiRilevantiRecuperati);
			System.out.println("Recall: " + recall);
			System.out.println("Precision: " + precision);
			System.out.println("F-Measure: " + fmeasure);

			id2url.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static boolean isRelevant(String[] alephredRow, String[] extractorRow, int threshold) throws UnsupportedEncodingException {

		int relevanceThreshold = threshold;
		int errorsDetected = 0;
		
		try{
			//title check
			String titleAlephred = alephredRow[1].replaceAll("[^\\p{ASCII}]", "").replaceAll("[^\\w]", "");
			String titleExtractor = extractorRow[1].replaceAll("[^\\p{ASCII}]", "").replaceAll("[^\\w]", "");
			if(!titleAlephred.equalsIgnoreCase(titleExtractor))  {
				System.err.println("Mismatch : title");
				errorsDetected += 1;
			}

			//directors check
			String directorAlephred = alephredRow[2].replaceAll("[^\\w]", "").replaceAll("[^\\p{ASCII}]", "");
			String directorExtractor = extractorRow[2].replaceAll("[^\\w]", "").replaceAll("[^\\p{ASCII}]", "");
			if(!directorExtractor.equalsIgnoreCase(directorAlephred)) {
				System.err.println("Mismatch : directors");
				errorsDetected += 1;
			}

			//genre check
			String genreAlephred = alephredRow[3].replaceAll("[^\\w]", "");
			String genreExtractor = extractorRow[3].replaceAll("[^\\w]", "");
			if(!genreExtractor.equalsIgnoreCase(genreAlephred)) {
				System.err.println("Mismatch : genre");
				errorsDetected += 1;
			}
			//year check
			String yearAlephred = alephredRow[4];
			String yearExtractor = extractorRow[4].replaceAll("ANNO: ", "");
			if(!yearExtractor.equalsIgnoreCase(yearAlephred))  {
				System.err.println("Mismatch : year");
				errorsDetected += 1;
			}

			//actors check
			String actorsAlephred = alephredRow[6].replaceAll("[^\\w]", "").replaceAll("[^\\p{ASCII}]", "");
			String actorsExtractor = extractorRow[6].replaceAll("ATTORI: ", "").replaceAll("[^\\p{ASCII}]", "").replaceAll("[^\\w]", "");
			if(!actorsExtractor.equalsIgnoreCase(actorsAlephred)) {
				System.err.println("Mismatch : actors");
				errorsDetected += 1;
			}

			//poster check
			String posterAlephred = alephredRow[7].replaceAll("[^\\p{ASCII}]", "");;
			String posterExtractor = extractorRow[7].replaceAll("[^\\p{ASCII}]", "");
			if(!posterExtractor.equalsIgnoreCase(posterAlephred)) {
				System.err.println("Mismatch : img");
				errorsDetected += 1;
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}

		if(errorsDetected >= relevanceThreshold){
			return false;
		}
		return true;

	}

	private static String[] findRowByID(List<String[]> alephredCsv, String id_in_csv) {
		for(int i=1; i<alephredCsv.size(); i++){
			String[] alephredRow = alephredCsv.get(i);
			String idAlephredRow = alephredRow[0];
			idAlephredRow = idAlephredRow.replaceAll("\\.html", "");
			if(id_in_csv.equals(idAlephredRow))
				return alephredRow;
		}
		return null; //non dovrebbe mai farlo!!
	}
}